package com.example.databindingdemo;

import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.databinding.BindingAdapter;

import com.squareup.picasso.Picasso;

/**
 * @author: tuyrt7 on 2020/9/8.
 */
public class ImageViewAdapter {

    private static final String TAG = "ImageViewAdapter";

    @BindingAdapter(value = {"image", "defaultImageResource"}, requireAll = false)
    public static void setImage(ImageView imageView, String imageUrl, int imageResource) {
        if (!TextUtils.isEmpty(imageUrl)) {
            Picasso.get().load(imageUrl)
                    .placeholder(R.mipmap.ic_launcher)
                    .error(R.mipmap.ic_launcher)
                    .into(imageView);
        } else {
            imageView.setImageResource(imageResource);
        }
    }

    @BindingAdapter("padding")
    public static void setPadding(View view, int oldPadding, int newPadding) {
        Log.e(TAG, "setPadding: oldPadding:" + oldPadding + "======newPadding:" + newPadding);
        if (oldPadding != newPadding) {
            view.setPadding(newPadding, newPadding, newPadding, newPadding);
        }
    }
}
